var user = {
  name: '袁进',
  birth: '2002-5-7'
}

observe(user)

// 显示姓氏
function showFirstName () {
  document.querySelector('#firstName').textContent = '姓：' + user.name[0]
}

// 显示名字
function showLastName () {
  document.querySelector('#lastName').textContent = '名：' + user.name.slice(1)
}

// 显示年龄
function showAge () {
  var birthday = new Date(user.birth)
  var today = new Date()
  today.setHours(0), today.setMinutes(0), today.setMilliseconds(0)
  thisYearBirthday = new Date(
    today.getFullYear(),
    birthday.getMonth(),
    birthday.getDate()
  )
  var age = today.getFullYear() - birthday.getFullYear()
  if (today.getTime() < thisYearBirthday.getTime()) {
    age--
  }
  document.querySelector('#age').textContent = '年龄：' + age
}

autorun(showFirstName)
autorun(showLastName)
autorun(showAge)
// window.__fun = showFirstName
// showFirstName()
// window.__fun = null
// window.__fun = showLastName
// showLastName()
// window.__fun = null
// window.__fun = showAge
// showAge()
// window.__fun = null

// user.name = '顿健康'
// let internalName = user.name
// Object.defineProperty(user, 'name', {
//   get: function () {
//     console.log('有人读取name属性', internalName)
//     return internalName
//   },
//   set: function (val) {
//     console.log('有人设置name属性', val)
//     internalName = val
//     showFirstName()
//     showLastName()
//   }
// })
// showFirstName()
// showLastName()
// user.birth = '1890-1-1'
// showAge()

function autorun (fn) {
  window.__fun = fn
  fn()
  window.__fun = null
}
